using LiJ.Common.Encrypt;
using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace LiJ.Framework.Msg
{
	/// <summary>
	/// 微信加密模式下使用的
	/// </summary>
	internal class Cryptography
	{
		public static int HostToNetworkOrder(int inval)
		{
			var num = 0;
			for (var i = 0; i < 4; i++)
			{
				num = (num << 8) + (inval >> i * 8 & 0xFF);
			}
			return num;
		}

		/// <summary>
		/// 解密方法
		/// </summary>
		/// <param name="Input">密文</param>
		/// <param name="encodingAesKey"></param>
		/// <returns></returns>
		public static string WxAesDecrypt(string Input, string encodingAesKey)
		{
			byte[] array = Convert.FromBase64String(encodingAesKey + "=");
			byte[] array2 = new byte[16];
			Array.Copy(array, array2, 16);
			byte[] toEncryptArray = Convert.FromBase64String(Input);
			byte[] array3 = Cryptography.Decode(AesRijndael.Decrypt(array, toEncryptArray, array2, 256, 128, CipherMode.CBC, PaddingMode.None));
			int network = BitConverter.ToInt32(array3, 16);
			network = IPAddress.NetworkToHostOrder(network);
			byte[] array4 = new byte[network];
			Array.Copy(array3, 20, array4, 0, network);
			return Encoding.UTF8.GetString(array4);
		}

		/// <summary>
		/// 加密返回的串
		/// </summary>
		/// <param name="input"></param>
		/// <param name="encodingAesKey"></param>
		/// <param name="appid"></param>
		/// <returns></returns>
		public static string AesEncrypt(string input, string encodingAesKey, string appid)
		{
			byte[] array = Convert.FromBase64String(encodingAesKey + "=");
			byte[] array2 = new byte[16];
			Array.Copy(array, array2, 16);
			string s = Cryptography.CreateRandCode(16);
			byte[] bytes = Encoding.UTF8.GetBytes(s);
			byte[] bytes2 = Encoding.UTF8.GetBytes(appid);
			byte[] bytes3 = Encoding.UTF8.GetBytes(input);
			byte[] bytes4 = BitConverter.GetBytes(Cryptography.HostToNetworkOrder(bytes3.Length));
			byte[] array3 = new byte[bytes.Length + bytes4.Length + bytes2.Length + bytes3.Length];
			Array.Copy(bytes, array3, bytes.Length);
			Array.Copy(bytes4, 0, array3, bytes.Length, bytes4.Length);
			Array.Copy(bytes3, 0, array3, bytes.Length + bytes4.Length, bytes3.Length);
			Array.Copy(bytes2, 0, array3, bytes.Length + bytes4.Length + bytes3.Length, bytes2.Length);
			byte[] array4 = new byte[array3.Length + 32 - array3.Length % 32];
			Array.Copy(array3, array4, array3.Length);
			byte[] array5 = Cryptography.Kcs7Encoder(array3.Length);
			Array.Copy(array5, 0, array4, array3.Length, array5.Length);
			return Convert.ToBase64String(AesRijndael.Encrypt(array, array4, array2, 256, 128, CipherMode.CBC, PaddingMode.None));
		}

		private static byte[] Kcs7Encoder(int textLength)
		{
			int num = 32 - textLength % 32;
			if (num == 0)
			{
				num = 32;
			}
			char c = Cryptography.Chr(num);
			string text = "";
			for (int i = 0; i < num; i++)
			{
				text += c.ToString();
			}
			return Encoding.UTF8.GetBytes(text);
		}

		private static char Chr(int a)
		{
			return (char)(byte)(a & 0xFF);
		}

		private static string CreateRandCode(int codeLen)
		{
			if (codeLen == 0)
			{
				codeLen = 16;
			}
			string[] array = "2,3,4,5,6,7,a,c,d,e,f,h,i,j,k,m,n,p,r,s,t,A,C,D,E,F,G,H,J,K,M,N,P,Q,R,S,U,V,W,X,Y,Z".Split(new char[1]
			{
				','
			});
			StringBuilder stringBuilder = new StringBuilder(codeLen);
			Random random = new Random((int)DateTime.Now.Ticks);
			for (int i = 0; i < codeLen; i++)
			{
				int num = random.Next(0, array.Length - 1);
				stringBuilder.Append(array[num]);
			}
			return stringBuilder.ToString();
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="decrypted"></param>
		/// <returns></returns>
		private static byte[] Decode(byte[] decrypted)
		{
			int num = decrypted[decrypted.Length - 1];
			if (num < 1 || num > 32)
			{
				num = 0;
			}
			byte[] array = new byte[decrypted.Length - num];
			Array.Copy(decrypted, 0, array, 0, decrypted.Length - num);
			return array;
		}
	}
}
